package operation;

import Beans.Student;
import User.User;
import Utils.SQLSentencePoll;
import dao.DataOperation;

import java.io.FileWriter;
import java.io.IOException;

/**
 * 数据库学生表信息导入到本地txt文件功能实现类
 */
public class PersonalInfoExport implements IOperation{
    @Override
    public boolean operate(User user) {
        System.out.println("个人信息导出...");
        FileWriter fw = null;
        try {
            //1.获取个人信息
            Student student = DataOperation.queryOne(Student.class, SQLSentencePoll.SpecifiedAccountInfoDisplay,
                    user.getAccountId());
            //2.导出到本地
            assert student != null;
            String info = student.getId() + "  " + student.getName() + "  " + student.getAge() + "  " +
                    student.getGender() + "\n";
            String path = "C:\\Users\\Jiali\\Desktop\\" + student.getName() + ".txt"; //指明文件路径
            fw = new FileWriter(path);
            fw.write(info);
            System.out.println("信息已导入桌面名为\""+ student.getName() + ".txt\"的文件中！");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //3.关闭流资源
            try {
                if(fw != null){
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
}
